package com.henry.customviewstudy.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.SweepGradient;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by henry on 2018/5/19.
 */

public class ScanRadar extends View {

    private Paint mPaintLine;
    private Paint mPaintCircle;
    private Paint mPaintSector;
    private Paint mPaintPoint;
    //雷达直径
    private float viewSize;
    //线程是否在执行
    private boolean isRunning = false;
    //任务是否已经开始
    private boolean isStart = false;

    //记录并设置旋转角度
    private int start = 0;

    //雷达旋转方向
    //顺时针
    public final static int CLOCK_WISE = 1;
    //逆时针
    public final static int ANSI_CLOCK_WISE = -1;
    public final static int DEFAULT_DIRECTION = CLOCK_WISE;

    private int direction = DEFAULT_DIRECTION;

    private ScanThread scanThread;  //线程
    private Matrix matrix;  //矩阵
    final int[] point_x = getRandomArray(15, 300);
    final int[] point_y = getRandomArray(15, 300);

    public ScanRadar(Context context) {
        this(context, null);
    }

    public ScanRadar(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ScanRadar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initPaint();
    }

    private void initPaint() {
        //用来绘画直线的画笔
        //宽度为5，抗锯齿，描边效果的白色画笔
        mPaintLine = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaintLine.setAntiAlias(true);
        mPaintLine.setColor(Color.WHITE);
        mPaintLine.setStyle(Paint.Style.STROKE);
        mPaintLine.setStrokeWidth(5);

        //用来绘画背景圆的画笔
        mPaintCircle = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaintCircle.setAntiAlias(true);
        mPaintCircle.setColor(0x99000000);
        //实心圆style
        mPaintCircle.setStyle(Paint.Style.FILL);

        //绘画圆渐变色的画笔
        mPaintSector = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaintSector.setAntiAlias(true);
        mPaintSector.setStyle(Paint.Style.FILL);
        //绘画实点
        mPaintPoint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaintPoint.setAntiAlias(true);
        mPaintPoint.setColor(Color.WHITE);
        mPaintPoint.setStyle(Paint.Style.FILL);

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //获得控件宽度 宽度<=高度
        viewSize = getMeasuredWidth();
        //初始化一个颜色渐变渲染器
        SweepGradient sweepGradient = new SweepGradient(viewSize / 2.0f, viewSize / 2.0f, Color.TRANSPARENT, Color.GREEN);
        mPaintSector.setShader(sweepGradient);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //绘制背景圆
        canvas.drawCircle(viewSize / 2.0f, viewSize / 2.0f, viewSize / 2.0f, mPaintCircle);
        //绘制空心圆
        canvas.drawCircle(viewSize / 2.0f, viewSize / 2.0f, viewSize / 4.0f, mPaintLine);
        canvas.drawCircle(viewSize / 2.0f, viewSize / 2.0f, viewSize / 8.0f, mPaintLine);
        canvas.drawCircle(viewSize / 2.0f, viewSize / 2.0f, viewSize / 2.0f, mPaintLine);
        //绘制水平垂直两个直径
        canvas.drawLine(0, viewSize / 2.0f, viewSize, viewSize / 2.0f, mPaintLine);
        canvas.drawLine(viewSize / 2.0f, 0, viewSize / 2.0f, viewSize, mPaintLine);
        //这里在雷达扫描过制定圆周度数后，将随机绘制一些白点，模拟搜索结果
        if (start > 100) {
            for (int i = 0; i < 2; i++) {
                canvas.drawCircle(viewSize / 2 + point_x[i], viewSize / 2 + point_y[i], 10, mPaintPoint);
            }
        }
        if (start > 200) {
            for (int i = 2; i < 5; i++) {
                canvas.drawCircle(viewSize / 2 + point_x[i], viewSize / 2 + point_y[i], 10, mPaintPoint);
            }
        }
        if (start > 300) {
            for (int i = 5; i < 9; i++) {
                canvas.drawCircle(viewSize / 2 + point_x[i], viewSize / 2 + point_y[i], 10, mPaintPoint);
            }
        }
        if (start > 500) {
            for (int i = 9; i < 11; i++) {
                canvas.drawCircle(viewSize / 2 + point_x[i], viewSize / 2 + point_y[i], 10, mPaintPoint);
            }
        }
        if (start > 800) {
            for (int i = 11; i < point_x.length; i++) {
                canvas.drawCircle(viewSize / 2 + point_x[i], viewSize / 2 + point_y[i], 10, mPaintPoint);
            }
        }

        //把画布的多有对象与matrix联系起来
        canvas.concat(matrix);
        //绘制颜色渐变圆 不断绘制shader，呈现出扇形扫描效果
        canvas.drawCircle(viewSize / 2.0f, viewSize / 2.0f, viewSize / 2.0f, mPaintSector);

    }

    //设置循环的方向
    public void setDirection(int d) {
        if (d != CLOCK_WISE && d != ANSI_CLOCK_WISE) {
            throw new IllegalStateException("only contonst CLOCK_WISE  ANSI_CLOCK_WISE");
        }
        this.direction = d;
    }

    //线程开启
    public void start() {
        scanThread = new ScanThread(this);
        scanThread.setName("radar");
        isRunning = true;
        isStart = true;
        scanThread.start();
    }

    //线程结束
    public void stop() {
        if (isStart) {
            isStart = false;
            isRunning = false;
        }
    }

    class ScanThread extends Thread {
        private View view;

        public ScanThread(View view) {
            super();
            this.view = view;
        }

        @Override
        public void run() {
            while (isRunning) {
                if (isStart) {
                    start += 1;
                    view.post(new Runnable() {
                        @Override
                        public void run() {
                            //创建一个矩阵
                            matrix = new Matrix();
                            //设置矩阵旋转
                            matrix.setRotate(start * direction, viewSize / 2.0f, viewSize / 2.0f);
                            //重画
                            view.invalidate();
                        }
                    });
                    try {
                        Thread.sleep(5);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
    /**
     * @param log 数组长度
     * @param top 随机数上限
     * @return 生成随机数数组，内容为[-top,top]
     */
    public static int[] getRandomArray(int log, int top) {
        int[] result = new int[log];
        for (int i = 0; i < log; i++) {
            int random = (int) (top * (2 * Math.random() - 1));
            result[i] = random;
        }
        return result;
    }
}
